package sg.edu.nus.comp.icreate.ivletalk.forum;

import java.util.List;
import sg.edu.nus.comp.icreate.ivletalk.ModuleNoRefreshBaseActivity;
import sg.edu.nus.comp.icreate.ivletalk.R;
import sg.edu.nus.comp.icreate.ivletalk.User;
import sg.edu.nus.comp.icreate.ivletalk.module.ElementType;
import sg.edu.nus.comp.icreate.ivletalk.module.MainActivity;
import sg.edu.nus.comp.icreate.ivletalk.utils.net.DataPoster;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

public class NewPostActivity extends ModuleNoRefreshBaseActivity {
	public static final String EXTRA_NAME_IS_REPLY = "sg.edu.nus.comp.icreate.ivletalk.extra.isReply";
	public static final String EXTRA_NAME_THREAD_HEADING_INDEX = "sg.edu.nus.comp.icreate.ivletalk.extra.threadHeadingIndex";
	public static final String EXTRA_NAME_THREAD_INDEX = "sg.edu.nus.comp.icreate.ivletalk.extra.threadIndex";
	public static final int REQUEST_CODE_NEW_POST = 1001;

	private static final String REPLY_HEADER = "Re:";

	private EditText titleView, bodyView;
	private Spinner headingSelect;
	private Button postButton;
	private int modulePosition;
	private List<Heading> headings;
	private boolean isReply;
	private String parentId;
	private String parentTitle;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_new_post);
		isReply = getIntent().getBooleanExtra(EXTRA_NAME_IS_REPLY, false);
		headingSelect = (Spinner) findViewById(R.id.headingSelect);
		if (isReply) {
			String id_title = getIntent().getStringExtra(ThreadActivity.EXTRA_NAME_PARENT_THREAD_ID_TITLE);
			int index = id_title.indexOf(ThreadActivity.THREAD_ID_TITLE_DIVIDER);
			parentId = id_title.substring(0, index);
			parentTitle = id_title.substring(index + 6);
			headingSelect.setVisibility(View.GONE);
		} else {
			modulePosition = getIntent().getIntExtra(MainActivity.EXTRA_NAME_MODULE_POSITION, 0);
			headings = ((Forum) MainActivity.modules.get(modulePosition).getElements().get(ElementType.FORUM)).getHeadings();
			headingSelect.setAdapter(new ArrayAdapter<Heading>(this, android.R.layout.simple_list_item_1, headings));
		}
		titleView = (EditText) findViewById(R.id.newPostTitle);
		bodyView = (EditText) findViewById(R.id.newPostBody);
		if (isReply()) {
			titleView.setText(REPLY_HEADER + parentTitle);
			bodyView.requestFocus();
		}
		postButton = (Button) findViewById(R.id.postButton);
		postButton.setOnClickListener(new PostButtonOnClickListener(this, headingSelect, titleView, bodyView));
	}

	public boolean isReply() {
		return isReply;
	}

	public void setReply(boolean isReply) {
		this.isReply = isReply;
	}

	public String getParentId() {
		return parentId;
	}

	public void setParentId(String parentId) {
		this.parentId = parentId;
	}

	private class PostButtonOnClickListener implements OnClickListener {
		private static final String KEY_APIKEY = "APIKey";
		private static final String KEY_TITLE = "Title";
		private static final String KEY_REPLY = "Reply";
		private static final String KEY_AUTH_TOKEN = "AuthToken";
		private static final String KEY_HEADING = "HeadingID";
		private static final String KEY_PARENT = "ThreadID";
		private static final String URL_REPLY = "https://ivle.nus.edu.sg/api/Lapi.svc/Forum_ReplyThread_JSON";
		private static final String URL_NEW_POST = "https://ivle.nus.edu.sg/api/Lapi.svc/Forum_PostNewThread_JSON";
		EditText titleView, bodyView;
		Spinner headingSelect;
		NewPostActivity hostActivity;

		public PostButtonOnClickListener(NewPostActivity newPostActivity, Spinner headingSelect, EditText title, EditText body) {
			this.titleView = title;
			this.bodyView = body;
			this.headingSelect = headingSelect;
			hostActivity = newPostActivity;
		}

		@Override
		public void onClick(View v) {
			String title = (String) titleView.getText().toString();
			String body = (String) bodyView.getText().toString();
			String url = hostActivity.isReply() ? URL_REPLY : URL_NEW_POST;
			DataPoster httpPoster = new DataPoster(url);
			httpPoster.addNameValuePair(KEY_AUTH_TOKEN, User.getInstance().getToken());
			httpPoster.addNameValuePair(KEY_APIKEY, MainActivity.APIKEY);
			httpPoster.addNameValuePair(KEY_TITLE, title);
			httpPoster.addNameValuePair(KEY_REPLY, body);
			if (hostActivity.isReply())
				httpPoster.addNameValuePair(KEY_PARENT, hostActivity.getParentId());
			else
				httpPoster.addNameValuePair(KEY_HEADING, ((Heading) headingSelect.getSelectedItem()).getId());
			httpPoster.execute();
			try {
				httpPoster.get();
				setResult(RESULT_OK);
				finish();
			} catch (Exception e) {
				Log.e("error", "Error in post data", e);
			}

		}

	}
}
